collector.ts ➔ collector   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 41
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 41
rs 7.712
c 0
b 0
f 0
cc 7
1
import type {CollectingArg, InternalOptions} from "./$defs.types"
2
import type {Options} from "./options.types"
3
4
export = collector
5
6
function collector(
7
  identifiers: Record<string, true>,
8
  {
9
    identifierParser,
10
    identifierMatchIndex,
11
    identifierCleanupParser,
12
    identifierCleanupReplace,
13
    allowedAtRuleNames
14
  }: Pick<Required<Options>, "identifierMatchIndex"|"identifierCleanupReplace">
15
  & Pick<InternalOptions, "identifierParser"|"identifierCleanupParser"|"allowedAtRuleNames">
16
) {
17
  return ({selectors, parent}: CollectingArg) => {
18
    if (parent?.type === "atrule") {
19
      const {name} = parent
20
21
      if (name && !allowedAtRuleNames.has(name))
22
        return
23
    }
24
25
    //TODO consider postcss-selector-parser
26
    const {length} = selectors
27
28
    for (let i = length; i--;) {
29
      const selector = selectors[i]
30
31
      let parsed: RegExpExecArray | null,
32
      lastIndex: number|undefined = undefined
33
34
      while (parsed = identifierParser.exec(selector)) {
35
        const {index} = parsed
36
37
        if (index === lastIndex)
38
          // TODO consider throw error
39
          return
40
41
        lastIndex = index
42
        const identifier = parsed[identifierMatchIndex]
43
        .replace(identifierCleanupParser, identifierCleanupReplace)
44
45
        identifiers[identifier] = true
46
      }
47
    }
48
  }
49
}
50
51